package org.jeecg.modules.qyxx.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.jeecg.modules.qyxx.entity.BgFpHzNdQtzyzc;
import org.jeecg.modules.qyxx.entity.BgFpTopXycp;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.qyxx.mapper.BgFpHzNdQtzyzcMapper;
import org.jeecg.modules.qyxx.service.IBgFpHzNdQtzyzcService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 其他重要支出年汇总数据
 * @Author: ZJJ
 * @Date: 2021-06-09
 * @Version: V1.0
 */
@Service
// @DS("multi-datasource1")申报
public class BgFpHzNdQtzyzcServiceImpl extends MppServiceImpl<BgFpHzNdQtzyzcMapper, BgFpHzNdQtzyzc> implements IBgFpHzNdQtzyzcService {
    @Autowired
    private BgFpHzNdQtzyzcMapper bgFpHzNdQtzyzcMapper;
    @Autowired
    private IBgSjmkjgOrderService bgSjmkjgOrderService;

    @Override
    public Map fpHzNdQtzyzc(String djxh, List<String> tjnds, Date jssj) {
        DateUtils utils = DateUtils.getInstance();
        // String year = bgSjmkjgOrderService.getNewByDjxh(djxh);
        String year = utils.getYearToStr(jssj);
        LambdaQueryWrapper<BgFpHzNdQtzyzc> wrapper = Wrappers.lambdaQuery(BgFpHzNdQtzyzc.class);
        // List<Object> tjnds =  bgFpHzNdQtzyzcMapper.selectObjs(wrapper);
        Map<String, Object> map = new LinkedHashMap<>();
        for (String tjnd : tjnds) {
            wrapper.clear();
            wrapper.eq(BgFpHzNdQtzyzc::getDjxh, djxh).eq(BgFpHzNdQtzyzc::getTjnd, tjnd);
            List<BgFpHzNdQtzyzc> maps = bgFpHzNdQtzyzcMapper.selectList(wrapper);
            if (maps != null) {
                for (BgFpHzNdQtzyzc bgFpHzNdQtzyzc : maps) {
                    if (bgFpHzNdQtzyzc.getSfzc() != null) {
                        bgFpHzNdQtzyzc.setSfzc(bgFpHzNdQtzyzc.getSfzc().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                    }
                    if (bgFpHzNdQtzyzc.getDfzc() != null) {
                        bgFpHzNdQtzyzc.setDfzc(bgFpHzNdQtzyzc.getDfzc().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                    }
                    if (bgFpHzNdQtzyzc.getRqzc() != null) {
                        bgFpHzNdQtzyzc.setRqzc(bgFpHzNdQtzyzc.getRqzc().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                    }
                    if (bgFpHzNdQtzyzc.getRlzc() != null) {
                        bgFpHzNdQtzyzc.setRlzc(bgFpHzNdQtzyzc.getRlzc().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                    }
                    if (bgFpHzNdQtzyzc.getYscczc() != null) {
                        bgFpHzNdQtzyzc.setYscczc(bgFpHzNdQtzyzc.getYscczc().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                    }
                    if (year.equals(bgFpHzNdQtzyzc.getTjnd())) {
                        // String month = bgSjmkjgOrderService.getNewMonthByDjxh(djxh);
                        String month = utils.getMonthToStr(jssj);
                        String newMonth = month.substring(month.length() - 2);
                       // bgFpHzNdQtzyzc.setTjnd(year + "年(" + newMonth + "月)");
                        map.put(year + "年(" + newMonth + "月)", maps);
                    } else {
                        map.put(bgFpHzNdQtzyzc.getTjnd() + "年", maps);
                        // bgFpHzNdQtzyzc.setTjnd(year + "年");
                    }
                }
                // map.put(tjnd, maps);
            }
        }
        return map;
    }
}
